require('babel-core/register')({
    'presets': ['node7']
});

let config = require('./config').default;
let http = require('http');

async function benchmark_koa(query, count) {
    let data = {
        query: query,
    };

    data = require('querystring').stringify(data);

    let opt = {
        method: "POST",
        host: "127.0.0.1",
        port: config.listen.koa,
        path: "/query",
        headers: {
            "Content-Type": 'application/x-www-form-urlencoded',
            "Content-Length": data.length
        }
    };

    let s = Date.now();
    let c = 1000;
    let num = 1;

    let ok = {};

    await (new Promise(function (resolver, reject) {
        for (let a = 0; a < c; a++) {
            setTimeout(() => {
                for (let i = 0; i < count/c; i++) {
                    await (new Promise(function (resolver, reject) {
                        let req = http.request(opt, function (res) {
                            if (res.statusCode == 200) {
                                var body = "";
                                res.on('data', function (data) { body += data; })
                                    .on('end', function () {
                                        resolver();
                                    });
                            }
                        });
                        req.write(data);
                        req.end();
                    }));

                    if (i !=0 && i % 1000 == 0) {
                        console.log(`请求次数: ${i}`);
                    }
                }
                ok[a] = true;

                for (let z = 0; z < c; z++) {
                    if (!ok[z]) return;
                }
                resolver();
            }, 0)

        }
    }));
    return s;
}


let dnode = require('dnode');

async function benchmark_dnode(query, count) {
    let t = Date.now();
    let c = 100;
    let num = 1;

    let ok = {};

    await (new Promise(function (resolver, reject) {
        for (let a = 0; a < c; a++) {
            let client = dnode.connect('192.168.100.3',config.listen.dnode);

            client.on('remote', (remote) => {
                (async () => {
                    for (let i = 1; i <= count/c; i++) {
                        await (new Promise(function (resolver, reject) {
                            remote.query(query, (str) => {
                                console.log(str)
                                if (str.errors) {reject(str.errors)}
                                resolver();
                            });
                        }));

                        if (num % 1000 == 0) {
                            console.log(`请求次数: ${num}`);
                        }
                        num++;
                    }

                    client.end();
                    ok[a] = true;

                    for (let z = 0; z < c; z++) {
                        if (!ok[z]) return;
                    }
                    resolver();
                })();
            });
        }
    }));

    return t;
}

let count = 10000;
let query = `
{
  test(limit:10) {
    id
    title
    user{
      username
      password
    }
  }
}`;

(async () => {
    console.log(`开始测试dnode`);
    let dnode_s = await benchmark_dnode(query, count);
    console.log(`总耗时: ${(Date.now()-dnode_s)/1000}s 单次耗时: ${(Date.now()-dnode_s)/count}ms 并发:${count/((Date.now()-dnode_s)/1000)|0}/s`)

    // console.log(`\n开始测试koa`);
    // let koa_s = await benchmark_koa(query, count);
    // console.log(`总耗时: ${(Date.now()-koa_s)/1000}s 单次耗时: ${(Date.now()-koa_s)/count}ms  并发:${count/((Date.now()-koa_s)/1000)|0}/s`)
})();
